package com.ds.topmenu;

import com.ds.bar.item.CodeGenTempItem;
import com.ds.bar.item.DomainTempItem;
import com.ds.bar.item.FormulaTypeBar;
import com.ds.common.JDSException;
import com.ds.common.util.StringUtility;
import com.ds.config.ErrorListResultModel;
import com.ds.config.ResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.enums.db.MethodChinaName;
import com.ds.esb.config.formula.EngineType;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.toolbar.dynbar.CustomToolsBar;
import com.ds.esd.custom.toolbar.dynbar.CustomTopToolsBar;
import com.ds.esd.custom.toolbar.dynbar.DynBar;
import com.ds.esd.dsm.enums.DSMType;
import com.ds.esd.editor.enums.CustomMenuType;
import com.ds.esd.editor.extmenu.PluginsFactory;
import com.ds.esd.tool.ui.component.UIItem;
import com.ds.esd.tool.ui.component.data.APICallerComponent;
import com.ds.esd.tool.ui.component.event.Action;
import com.ds.esd.tool.ui.component.event.Event;
import com.ds.esd.tool.ui.component.navigation.ToolBarProperties;
import com.ds.esd.tool.ui.enums.event.enums.ToolBarEventEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


@Controller
@RequestMapping(value = {"/action/spatoolbar/"})
@MethodChinaName(cname = "RAD菜单")
public class TopMenuAction {
    @RequestMapping(method = RequestMethod.POST, value = "Formula")
    @ModuleAnnotation(dynLoad = true, imageClass = "bpmfont bpmgongzuoliu")
    @ResponseBody
    public TreeListResultModel<List<FormulaTypeBar>> getFormulaMenu(String id, EngineType engineType) {
        TreeListResultModel<List<FormulaTypeBar>> resultModel = new TreeListResultModel<List<FormulaTypeBar>>();
        List<FormulaTypeBar> items = new ArrayList<>();
        FormulaTypeBar root = new FormulaTypeBar(engineType);
        items.add(root);
        if (id != null && !id.equals("")) {
            String[] orgIdArr = StringUtility.split(id, ";");
            resultModel.setIds(Arrays.asList(orgIdArr));
        } else {
            resultModel.setIds(Arrays.asList(new String[]{root.getFristClassItem(root).getId()}));
        }
        resultModel.setData(items);
        return resultModel;

    }

    @MethodChinaName(cname = "出码方案")
    @RequestMapping(method = RequestMethod.POST, value = "GenCode")
    @ModuleAnnotation(dynLoad = true, imageClass = "spafont spa-icon-formatjs")
    @ResponseBody
    public TreeListResultModel<List<CodeGenTempItem>> getGenCodeType(String id) {
        TreeListResultModel<List<CodeGenTempItem>> resultModel = new TreeListResultModel<List<CodeGenTempItem>>();
        CodeGenTempItem viewItem = null;
        try {
            viewItem = new CodeGenTempItem();
            List<CodeGenTempItem> items = new ArrayList<>();
            items.add(viewItem);
            if (id != null && !id.equals("")) {
                String[] orgIdArr = StringUtility.split(id, ";");
                resultModel.setIds(Arrays.asList(orgIdArr));
            } else {
                resultModel.setIds(Arrays.asList(new String[]{viewItem.getFristClassItem(viewItem).getId()}));
            }
            resultModel.setData(items);
        } catch (JDSException e) {
            resultModel = new ErrorListResultModel<>();
            ((ErrorListResultModel<List<CodeGenTempItem>>) resultModel).setErrdes(e.getMessage());
        }
        return resultModel;
    }

    @MethodChinaName(cname = "领域模型")
    @RequestMapping(method = RequestMethod.POST, value = "Domain")
    @ModuleAnnotation(dynLoad = true, imageClass = "spafont spa-icon-formatjs")
    @ResponseBody
    public TreeListResultModel<List<DomainTempItem>> getDomain(String id) {
        TreeListResultModel<List<DomainTempItem>> resultModel = new TreeListResultModel<List<DomainTempItem>>();
        DomainTempItem viewItem = null;
        try {
            viewItem = new DomainTempItem(DSMType.customDomain);
            List<DomainTempItem> items = new ArrayList<>();
            items.add(viewItem);
            if (id != null && !id.equals("")) {
                String[] orgIdArr = StringUtility.split(id, ";");
                resultModel.setIds(Arrays.asList(orgIdArr));
            } else {
                resultModel.setIds(Arrays.asList(new String[]{viewItem.getFristClassItem(viewItem).getId()}));
            }
            resultModel.setData(items);
        } catch (JDSException e) {
            resultModel = new ErrorListResultModel<>();
            ((ErrorListResultModel<List<DomainTempItem>>) resultModel).setErrdes(e.getMessage());
        }
        return resultModel;
    }

    @ResponseBody
    @RequestMapping(value = {"getPluginBar"}, method = {RequestMethod.POST, RequestMethod.GET})
    public ResultModel<CustomTopToolsBar> getPluginBar(CustomMenuType menuType, String pattern, String projectName) {
        ResultModel<CustomTopToolsBar> resultModel = new ResultModel<CustomTopToolsBar>();
        CustomTopToolsBar<ToolBarProperties, ToolBarEventEnum> toolsBar = new CustomTopToolsBar("TopBar");
        try {
            List<DynBar> viewBars = PluginsFactory.getInstance().getAllViewBar(menuType);
            for (DynBar bar : viewBars) {
                if (bar instanceof CustomToolsBar) {
                    CustomToolsBar<ToolBarProperties, ToolBarEventEnum> childBar = PluginsFactory.getInstance().getViewBarById(bar.getId(), null);
                    Set<Map.Entry<ToolBarEventEnum, Event>> entrySet = childBar.getEvents().entrySet();
                    for (Map.Entry<ToolBarEventEnum, Event> entry : entrySet) {
                        for (Action action : entry.getValue().getActions()) {
                            toolsBar.addEvent((ToolBarEventEnum) entry.getValue().eventKey, action);
                        }
                    }
                    List<UIItem> items = childBar.getProperties().getItems();
                    for (UIItem item : items) {
                        toolsBar.getProperties().addItem(item);
                    }
                    Set<APICallerComponent> apiCallerComponents = childBar.getApis();
                    for (APICallerComponent api : apiCallerComponents) {
                        toolsBar.getApis().add(api);
                    }
                }
            }
            resultModel.setData(toolsBar);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return resultModel;
    }


}
